//
//  ProductDetailImgCell.m
//  LazyCat
//
//  Created by Abner on 16/4/22.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "ProductDetailImgCell.h"
#import "UIImageView+WebCache.h"
#import "AddProduct.h"
#define qiangW 34

@interface ProductDetailImgCell ()
{
    UILabel *_lcPriceLB; //懒猫价lb
    
    
    UIView *_middleBgV;

}
@end
@implementation ProductDetailImgCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self creatUI];
    }
    return self;
}
-(void)creatUI
{
    self.proImgView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.proImgView];
    
    _middleBgV = [[UIView alloc]init];
    _middleBgV.backgroundColor = BackColor;
    [self.contentView addSubview:_middleBgV];
    
    self.pronameLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.pronameLB];
    self.pronameLB.textColor = SetColorRGBValue(0x000000, 1.0);
    self.pronameLB.font = FontSet(16);
    self.pronameLB.numberOfLines = 0;
    self.pronameLB.text = @"";
    
    //懒猫价lb
    _lcPriceLB = [[UILabel alloc]init];
    [self.contentView addSubview:_lcPriceLB];
    _lcPriceLB.font = [UIFont systemFontOfSize:13];
    _lcPriceLB.textColor = SetColorRGBValue(0x999999, 1.0);
    _lcPriceLB.text = @"懒猫价:";
    
    self.proPriceLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.proPriceLB];
    self.proPriceLB.font = [UIFont boldSystemFontOfSize:16];
    self.proPriceLB.textColor = SetColorRGBValue(0xff6600, 1.0);
    
    
    self.proPriceLB1 = [[UILabel alloc]init];
    [self.contentView addSubview:self.proPriceLB1];
    self.proPriceLB1.font = [UIFont boldSystemFontOfSize:20];
    self.proPriceLB1.textColor = SetColorRGBValue(0xff6600, 1.0);
    self.proPriceLB1.text = @"";
    
    
    self.salePriceLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.salePriceLB];
    self.salePriceLB.font = FontSet(12);
    self.salePriceLB.textColor = SetColorRGBValue(0x999999, 1.0);
    self.salePriceLB.text = @"";
    
    self.proMarketPriceLB = [[UILabel alloc]init];
    [self.contentView addSubview:self.proMarketPriceLB];
    self.proMarketPriceLB.font = FontSet(12);
    self.proMarketPriceLB.textColor = SetColorRGBValue(0x999999, 1.0);
    self.proMarketPriceLB.text = @"";

    

    
}
#pragma mark --- 没有懒猫券价
-(void)setupFrame1:(LCProductSpec *)aSpec
{
    self.proPriceLB.hidden = NO;
    self.proPriceLB1.hidden = NO;
    
    self.proPriceLB.frame = CGRectMake(10, CGRectGetMaxY(self.pronameLB.frame)+8, 10, 20);
    self.proPriceLB.text = @"¥";
    CGSize LCsize = [self.proPriceLB1.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.proPriceLB1.font,NSFontAttributeName, nil]];
    self.proPriceLB1.frame = CGRectMake(20, CGRectGetMaxY(self.pronameLB.frame)+8, LCsize.width+1, 20);
    if ([aSpec.marketprice floatValue] <= [self.proPriceLB1.text floatValue] || [aSpec.marketprice floatValue] <= 0) {
        self.proMarketPriceLB.hidden = YES;
    }else{
        self.proMarketPriceLB.hidden = NO;
        self.proMarketPriceLB.text = [NSString stringWithFormat:@"市场价: ¥%@",aSpec.marketprice];
        // 25 = 10 +15
        self.proMarketPriceLB.frame = CGRectMake(self.proPriceLB1.right+10, self.proPriceLB1.frame.origin.y+4, ScreenWidth-LCsize.width-1-35, 12);
    }
   
}

-(void)spec_setupProductDetailImgCell:(AddProduct *)aProductDetailModel currentSpecIndex:(NSInteger)currentSpecIndex
{
    
    self.proPriceLB.hidden = YES;
    self.proPriceLB1.hidden = YES;
    self.proMarketPriceLB.hidden = YES;
    self.salePriceLB.hidden = YES;
    _lcPriceLB.hidden = YES;
    
    //多规格的话，默认显示第一种的
    LCProductSpec *aSpec = [[LCProductSpec alloc]init];
    if (aProductDetailModel.productspec.count > currentSpecIndex) {
        aSpec = aProductDetailModel.productspec[currentSpecIndex];
    }
    
    [self.proImgView sd_setImageWithURL:[NSURL URLWithString:aSpec.mainimage] placeholderImage:[UIImage imageNamed:@"fanglanmao.png"]];
    self.proImgView.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth);
    
    _middleBgV.frame = CGRectMake(0, self.proImgView.bottom, ScreenWidth, 5);
    //产品名称
    if (aProductDetailModel.productname.length <= 0) {
        self.pronameLB.text =@" ";
    }else{
        self.pronameLB.text = aProductDetailModel.productname;
    }
    CGFloat namehH = [self getHeight:aProductDetailModel.productname fontSize:16 strwidth:ScreenWidth-20];
    self.pronameLB.numberOfLines = 0;
    self.pronameLB.frame = CGRectMake(10, _middleBgV.bottom+15, ScreenWidth-20, namehH);
    
    NSString *saleStr ;
    if(aSpec.saleprice.length > 0){
        saleStr = [NSString stringWithFormat:@"%@",[aSpec.saleprice safeString]];
    }else{
        saleStr = @"";
    }
    self.proPriceLB1.text = saleStr;
    [self setupFrame1:aSpec];
    
}
#pragma mark -- 计算文字宽
-(CGSize)calculateSizeWithText:(NSString *)text height:(CGFloat)height fontSize:(CGFloat)fontSize
{
    CGSize size = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return size;
}
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *indetifier = @"ProductDetailImgCellIdentifier";
    ProductDetailImgCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[ProductDetailImgCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(CGFloat)getHeight:(NSString *)str fontSize:(CGFloat)fontSize strwidth:(CGFloat)strwidth{
    CGSize aSize = [str boundingRectWithSize:CGSizeMake(strwidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return aSize.height;
}

-(CGFloat)getWidth:(NSString *)str fontSize:(CGFloat)fontSize strHeight:(CGFloat)strheight{
    CGSize aSize = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, strheight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return aSize.width;
}

@end
